package com.itheima.day15.work5;

import java.io.*;
import java.net.Socket;

public class ServerThread implements  Runnable{
    private Socket accept;
    private BufferedWriter bw;


    public ServerThread(Socket accept, BufferedWriter bw) {
        this.accept=accept;
        this.bw=bw;
    }
    @Override
    public void run() {
       BufferedReader br= null;
        try {
             br = new BufferedReader(new InputStreamReader(accept.getInputStream()));
            //BufferedWriter bw=new BufferedWriter(new FileWriter("a.txt"));
            String  s;
            while ((s=br.readLine())!=null){
                System.out.println("服务器数据："+s);
                bw.write(s);
                bw.newLine();
                bw.flush();
            }

            BufferedWriter bw1=new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
            bw1.write("上传成功");
            bw1.newLine();
            bw1.flush();


        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (br!=null){
                try {
                    br.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (accept!=null){
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }
    }
}

